# MCP Memory System Makefile

# 变量定义
PYTHON := python
PIP := pip
DOCKER := docker
DOCKER_COMPOSE := docker-compose
PROJECT_NAME := mcp-memory-system
VERSION := 1.0.0

# 颜色定义
RED := \033[31m
GREEN := \033[32m
YELLOW := \033[33m
BLUE := \033[34m
RESET := \033[0m

# 默认目标
.DEFAULT_GOAL := help

# 帮助信息
.PHONY: help
help: ## 显示帮助信息
	@echo "$(BLUE)MCP Memory System - 可用命令:$(RESET)"
	@echo ""
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "$(GREEN)%-20s$(RESET) %s\n", $$1, $$2}'

# 开发环境设置
.PHONY: install
install: ## 安装项目依赖
	@echo "$(YELLOW)安装项目依赖...$(RESET)"
	$(PIP) install -r requirements.txt
	@echo "$(GREEN)依赖安装完成$(RESET)"

.PHONY: install-dev
install-dev: ## 安装开发依赖
	@echo "$(YELLOW)安装开发依赖...$(RESET)"
	$(PIP) install -r requirements.txt
	$(PIP) install pytest pytest-asyncio pytest-cov black flake8 mypy isort pre-commit
	@echo "$(GREEN)开发依赖安装完成$(RESET)"

.PHONY: setup
setup: ## 设置开发环境
	@echo "$(YELLOW)设置开发环境...$(RESET)"
	$(PYTHON) -m venv venv
	@echo "$(GREEN)虚拟环境创建完成$(RESET)"
	@echo "$(BLUE)请激活虚拟环境: venv\\Scripts\\activate (Windows) 或 source venv/bin/activate (Linux/Mac)$(RESET)"

# 代码质量
.PHONY: format
format: ## 格式化代码
	@echo "$(YELLOW)格式化代码...$(RESET)"
	black src/ tests/
	isort src/ tests/
	@echo "$(GREEN)代码格式化完成$(RESET)"

.PHONY: lint
lint: ## 代码检查
	@echo "$(YELLOW)执行代码检查...$(RESET)"
	flake8 src/ tests/
	mypy src/
	@echo "$(GREEN)代码检查完成$(RESET)"

.PHONY: check
check: format lint ## 格式化并检查代码
	@echo "$(GREEN)代码质量检查完成$(RESET)"

# 测试
.PHONY: test
test: ## 运行测试
	@echo "$(YELLOW)运行测试...$(RESET)"
	pytest
	@echo "$(GREEN)测试完成$(RESET)"

.PHONY: test-unit
test-unit: ## 运行单元测试
	@echo "$(YELLOW)运行单元测试...$(RESET)"
	pytest -m unit
	@echo "$(GREEN)单元测试完成$(RESET)"

.PHONY: test-integration
test-integration: ## 运行集成测试
	@echo "$(YELLOW)运行集成测试...$(RESET)"
	pytest -m integration
	@echo "$(GREEN)集成测试完成$(RESET)"

.PHONY: test-cov
test-cov: ## 运行测试并生成覆盖率报告
	@echo "$(YELLOW)运行测试并生成覆盖率报告...$(RESET)"
	pytest --cov=src --cov-report=html --cov-report=term
	@echo "$(GREEN)测试覆盖率报告生成完成$(RESET)"

# 运行应用
.PHONY: run
run: ## 运行应用
	@echo "$(YELLOW)启动MCP Memory System...$(RESET)"
	$(PYTHON) src/main.py

.PHONY: run-dev
run-dev: ## 开发模式运行应用
	@echo "$(YELLOW)开发模式启动MCP Memory System...$(RESET)"
	PYTHONPATH=src $(PYTHON) src/main.py

# Docker相关
.PHONY: docker-build
docker-build: ## 构建Docker镜像
	@echo "$(YELLOW)构建Docker镜像...$(RESET)"
	$(DOCKER) build -t $(PROJECT_NAME):$(VERSION) .
	$(DOCKER) tag $(PROJECT_NAME):$(VERSION) $(PROJECT_NAME):latest
	@echo "$(GREEN)Docker镜像构建完成$(RESET)"

.PHONY: docker-run
docker-run: ## 运行Docker容器
	@echo "$(YELLOW)运行Docker容器...$(RESET)"
	$(DOCKER) run -d --name $(PROJECT_NAME) -p 8765:8765 $(PROJECT_NAME):latest
	@echo "$(GREEN)Docker容器启动完成$(RESET)"

.PHONY: docker-stop
docker-stop: ## 停止Docker容器
	@echo "$(YELLOW)停止Docker容器...$(RESET)"
	$(DOCKER) stop $(PROJECT_NAME) || true
	$(DOCKER) rm $(PROJECT_NAME) || true
	@echo "$(GREEN)Docker容器已停止$(RESET)"

# Docker Compose相关
.PHONY: up
up: ## 启动所有服务
	@echo "$(YELLOW)启动所有服务...$(RESET)"
	$(DOCKER_COMPOSE) up -d
	@echo "$(GREEN)所有服务启动完成$(RESET)"

.PHONY: up-dev
up-dev: ## 启动开发环境服务
	@echo "$(YELLOW)启动开发环境服务...$(RESET)"
	$(DOCKER_COMPOSE) --profile development up -d
	@echo "$(GREEN)开发环境服务启动完成$(RESET)"

.PHONY: up-prod
up-prod: ## 启动生产环境服务
	@echo "$(YELLOW)启动生产环境服务...$(RESET)"
	$(DOCKER_COMPOSE) --profile production up -d
	@echo "$(GREEN)生产环境服务启动完成$(RESET)"

.PHONY: up-monitoring
up-monitoring: ## 启动监控服务
	@echo "$(YELLOW)启动监控服务...$(RESET)"
	$(DOCKER_COMPOSE) --profile monitoring up -d
	@echo "$(GREEN)监控服务启动完成$(RESET)"

.PHONY: down
down: ## 停止所有服务
	@echo "$(YELLOW)停止所有服务...$(RESET)"
	$(DOCKER_COMPOSE) down
	@echo "$(GREEN)所有服务已停止$(RESET)"

.PHONY: logs
logs: ## 查看服务日志
	@echo "$(YELLOW)查看服务日志...$(RESET)"
	$(DOCKER_COMPOSE) logs -f

.PHONY: status
status: ## 查看服务状态
	@echo "$(YELLOW)查看服务状态...$(RESET)"
	$(DOCKER_COMPOSE) ps

# 数据库相关
.PHONY: db-up
db-up: ## 启动数据库服务
	@echo "$(YELLOW)启动数据库服务...$(RESET)"
	$(DOCKER_COMPOSE) up -d redis mongodb neo4j
	@echo "$(GREEN)数据库服务启动完成$(RESET)"

.PHONY: db-down
db-down: ## 停止数据库服务
	@echo "$(YELLOW)停止数据库服务...$(RESET)"
	$(DOCKER_COMPOSE) stop redis mongodb neo4j
	@echo "$(GREEN)数据库服务已停止$(RESET)"

.PHONY: db-reset
db-reset: ## 重置数据库数据
	@echo "$(RED)警告: 这将删除所有数据库数据!$(RESET)"
	@read -p "确认继续? [y/N] " confirm && [ "$$confirm" = "y" ] || exit 1
	$(DOCKER_COMPOSE) down -v
	$(DOCKER) volume prune -f
	@echo "$(GREEN)数据库数据已重置$(RESET)"

# 清理
.PHONY: clean
clean: ## 清理临时文件
	@echo "$(YELLOW)清理临时文件...$(RESET)"
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -delete
	find . -type d -name ".pytest_cache" -delete
	rm -rf htmlcov/
	rm -rf .coverage
	rm -rf dist/
	rm -rf build/
	rm -rf *.egg-info/
	@echo "$(GREEN)临时文件清理完成$(RESET)"

.PHONY: clean-all
clean-all: clean docker-stop ## 清理所有文件和容器
	@echo "$(YELLOW)清理Docker镜像...$(RESET)"
	$(DOCKER) rmi $(PROJECT_NAME):latest $(PROJECT_NAME):$(VERSION) || true
	@echo "$(GREEN)完全清理完成$(RESET)"

# 配置
.PHONY: config
config: ## 创建配置文件
	@echo "$(YELLOW)创建配置文件...$(RESET)"
	@if [ ! -f config/config.yaml ]; then \
		cp config/config.example.yaml config/config.yaml; \
		echo "$(GREEN)配置文件已创建: config/config.yaml$(RESET)"; \
	else \
		echo "$(BLUE)配置文件已存在: config/config.yaml$(RESET)"; \
	fi

# 健康检查
.PHONY: health
health: ## 检查服务健康状态
	@echo "$(YELLOW)检查服务健康状态...$(RESET)"
	@curl -s http://localhost:8765/health || echo "$(RED)服务不可用$(RESET)"

# 备份和恢复
.PHONY: backup
backup: ## 备份数据
	@echo "$(YELLOW)备份数据...$(RESET)"
	mkdir -p backups
	$(DOCKER_COMPOSE) exec mongodb mongodump --out /tmp/backup
	$(DOCKER) cp $$($(DOCKER_COMPOSE) ps -q mongodb):/tmp/backup ./backups/mongodb-$$(date +%Y%m%d_%H%M%S)
	@echo "$(GREEN)数据备份完成$(RESET)"

# 文档
.PHONY: docs
docs: ## 生成文档
	@echo "$(YELLOW)生成文档...$(RESET)"
	@echo "$(BLUE)文档功能待实现$(RESET)"

# 发布
.PHONY: build
build: clean ## 构建分发包
	@echo "$(YELLOW)构建分发包...$(RESET)"
	$(PYTHON) setup.py sdist bdist_wheel
	@echo "$(GREEN)分发包构建完成$(RESET)"

.PHONY: release
release: build ## 发布到PyPI
	@echo "$(YELLOW)发布到PyPI...$(RESET)"
	@echo "$(BLUE)发布功能待实现$(RESET)"

# 初始化项目
.PHONY: init
init: setup install-dev config ## 初始化项目
	@echo "$(GREEN)项目初始化完成!$(RESET)"
	@echo "$(BLUE)下一步:$(RESET)"
	@echo "  1. 激活虚拟环境"
	@echo "  2. 编辑 config/config.yaml"
	@echo "  3. 运行 make db-up 启动数据库"
	@echo "  4. 运行 make run 启动应用"

# 快速开始
.PHONY: quickstart
quickstart: db-up ## 快速开始（启动数据库和应用）
	@echo "$(YELLOW)等待数据库启动...$(RESET)"
	sleep 10
	@echo "$(YELLOW)启动应用...$(RESET)"
	$(MAKE) run-dev

# 开发工作流
.PHONY: dev
dev: check test ## 开发工作流（检查代码并测试）
	@echo "$(GREEN)开发工作流完成$(RESET)"

# CI/CD
.PHONY: ci
ci: install-dev check test-cov ## CI流水线
	@echo "$(GREEN)CI流水线完成$(RESET)"

.PHONY: cd
cd: docker-build ## CD流水线
	@echo "$(GREEN)CD流水线完成$(RESET)"